package com.ycjy.szteacher.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ycjy.szteacher.entity.Student;
import com.ycjy.szteacher.entity.Teacher;
import com.ycjy.szteacher.mapper.StudentMapper;
import com.ycjy.szteacher.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘洋
 * @since 2020-10-09
 */
@SuppressWarnings("ALL")
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/seleAllListStudent")
    @ResponseBody
    public PageUtil<Student> seleAllListStudent(String studentName, String studentNo, String studentSex) {
        Student student = new Student();
        if (studentNo == null || studentNo.equals("")) {
            student.setStudentNo(null);
        } else {
            student.setStudentNo(Integer.parseInt(studentNo));
        }
        if (studentSex == null || studentSex.equals("")) {
            student.setStudentSex(null);
        } else {
            student.setStudentSex(studentSex);
        }
        if (studentName == null || studentName.equals("")) {
            student.setStudentName(null);
        } else {
            student.setStudentName(studentName);
        }
        QueryWrapper<Student> wrapper = new QueryWrapper<>(student);

        Integer integer = studentMapper.selectCount(wrapper);
        //数据数量
        long count = (long) integer;
        PageUtil<Student> pageUtil = new PageUtil();
        List<Student> students = studentMapper.selectList(wrapper);
        pageUtil.setData(students);
        pageUtil.setCount(count);
        return pageUtil;
    }

    @RequestMapping("/seleOneStudent")
    public String seleOneStudent(int studentNo, Model model) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("studentNo", studentNo);
        Student student = studentMapper.selectOne(wrapper);
        model.addAttribute("student", student);
        return "/page/table/edit";
    }

    @RequestMapping("/removeStudent")
    @ResponseBody
    public int removeStudent(int studentNo) {
        int i = studentMapper.deleteById(studentNo);
        if (i > 0) {
            return 1;
        } else {
            return 2;
        }
    }

    @RequestMapping("/updateStudent")
    @ResponseBody
    public int updateStudent(@RequestBody(required = true)Student student) {
        int i = studentMapper.updateById(student);
        if (i > 0) {
            return 1;
        } else {
            return 2;
        }
    }
    @RequestMapping("/insertStudent")
    @ResponseBody
    public int insertStudent(@RequestBody(required = true)Student student) {
        int i = studentMapper.insert(student);
        if (i > 0) {
            return 1;
        } else {
            return 2;
        }
    }

}

